-- cmd命令处理中心
local ngx 		= ngx
local string_match = string.match
local tonumber = tonumber

local mis_uu = require('modules.commhm.utils')
local mis_timer = require('modules.commhm.ngxTimerWrap')
local log        = require("modules.commhm.log");
local table=table
local io = io
local uu = mis_uu
local mis_data = nil
local mis_config = nil
local mis_log = nil
local mis_common = nil
local mis_saveAss = nil
local mis_act = nil
local mis_calcu = nil
local t_retCode = nil
local mis_lock = nil
local mis_bridge=nil
local mis_awakening = nil
--------------------------------------------------------------
local mis_cmd = {
	s_name = string.match(debug.getinfo(1).short_src, '.*/(.*).lua');	-- 模块名称
}
local this = mis_cmd

--------------------------------------------------------------
-- 说明：模块包含
-- 参数：模块表
this.modules_include = function(t_moddules)
	mis_config = t_moddules.mis_config
	mis_data = t_moddules.mis_data
	mis_log = t_moddules.mis_log
	mis_common = t_moddules.mis_common
	mis_saveAss = t_moddules.mis_saveAss
	mis_act = t_moddules.mis_act
	mis_calcu = t_moddules.mis_calcu
	t_retCode = mis_config.t_retCode
	mis_lock = t_moddules.mis_lock
	mis_bridge=t_moddules.mis_bridge
	mis_awakening = t_moddules.mis_awakening

	this.set_awakening_share_item = mis_awakening.setting_item
	this.reset_awakening_data = mis_awakening.reset_awakening_data
	this.update_awakening_cloud_dhs = mis_awakening.update_could_room_dhs
	this.pop_awakening_ticket = mis_awakening.pop_awakening_ticket
	this.fix_awakening_rank = mis_awakening.fix_rank_value
	this.drop_awakening_rank = mis_awakening.drop_from_rank
end

--------------------------------------------------------------
-- 说明：加载配置
-- 返回：n		状态码
--		s	 	描述
this.load_config = function()
	return mis_data.load_config()
end

--------------------------------------------------------------
-- 说明：增加玩家宝库货币
-- 参数：id					货币Id
-- 返回：n					状态码
--		s 					描述
this.baoku_gain = function()
	if mis_data.t_baoku.n_status ~= mis_config.t_workStatus.n_ENABLE then
		return t_retCode.n_OK, 0
	end

	local t_params = ngx.ctx.t_params
	local n_baokuId = mis_data.t_baoku.t_conf.coin[t_params.id]

	if t_params.num <= 0 then
		return t_retCode.n_PARAM_ERR, 'invalid num'
	end
	if not n_baokuId then
		return t_retCode.n_PARAM_ERR, 'invalid id'
	end

	local t_user = mis_data.get_user()
	local n_code, t_baokuData = t_user:get_baoku_data()
	if n_code ~= t_retCode.n_OK then
		return n_code, t_baokuData
	end

	t_baokuData[n_baokuId] = t_baokuData[n_baokuId] or {}
	t_baokuData[n_baokuId].coin = t_baokuData[n_baokuId].coin and t_baokuData[n_baokuId].coin + t_params.num or t_params.num

	local n_code, s_desc = t_user:set_baoku_data(t_baokuData)
	if n_code ~= t_retCode.n_OK then
		return n_code, s_desc
	end

	mis_log.remote_day('baoku', 'gain|' .. t_params.uin .. '|' .. n_baokuId .. '|' .. t_params.num .. '|' .. t_baokuData[n_baokuId].coin)

	return t_retCode.n_OK, t_baokuData[n_baokuId].coin
end

--------------------------------------------------------------
-- 说明：奥特曼活动数据下载
-- 参数：id 					地图id
-- 返回：n					状态码
--		ts 					描述
this.outman_download = function()
	local t_params = ngx.ctx.t_params
	local n_code, t_outmanData = mis_data.get_user():get_outman_data()
	if n_code ~= t_retCode.n_OK then
		return n_code, t_outmanData
	end
	return t_outmanData[t_params.id] and t_retCode.n_OK or t_retCode.n_NO_DATA, t_outmanData[t_params.id]
end

--------------------------------------------------------------
-- 说明：事件上报
-- 参数：event 				事件id
-- 		value 				增量
-- 返回：n					状态码
--		ts 					描述
this.mission_event = function()
	local n_code, s_desc = mis_act.pre_mission()
	if n_code ~= t_retCode.n_OK then
		return n_code, s_desc
	end
	return mis_act.mission_event()
end

--------------------------------------------------------------
-- 说明：获得转盘活动时间
-- 返回：n					状态码
--		s 					描述
this.rotate_time = function()
	return t_retCode.n_OK, {
		startTime = mis_data.t_rotate.t_conf.n_startTime;
		endTime = mis_data.t_rotate.t_conf.n_endTime;
	}
end

--------------------------------------------------------------
-- 说明：查看玩家数据
-- 参数：key 				数据名
-- 返回：n					状态码
--		s 					描述
this.show_user_data = function()
	local t_user = mis_data.get_user()
	return t_user['get_' .. ngx.ctx.t_params.key .. '_data'](t_user)
end

this.show_user_data2 = function()
	local t_params = ngx.ctx.t_params
	local t_user = mis_data.get_user()
	return t_user['get_' .. ngx.ctx.t_params.key .. '_data'](t_user, t_params.type)
end

--------------------------------------------------------------
-- 说明：查看玩家shared_dict数据
-- 参数：key 				数据名
-- 返回：n					状态码
--		s 					描述
this.show_user_sd = function()
	local t_params = ngx.ctx.t_params
	return t_retCode.n_OK, ngx.shared[t_params.key]:get(t_params.uin .. '_' .. t_params.key) or 'nil'
end

this.show_user_sd2 = function()
	local t_params = ngx.ctx.t_params
	return t_retCode.n_OK, ngx.shared[t_params.key]:get(t_params.hash) or 'nil'
end

--------------------------------------------------------------
-- 说明：清空shared_dict数据
-- 参数：key 				数据名
-- 返回：n					状态码
--		s 					描述
this.flush_all_sd = function()
	local t_params = ngx.ctx.t_params
	ngx.shared[t_params.key]:flush_all()
	return t_retCode.n_OK
end

--------------------------------------------------------------
-- 说明：清空玩家数据
-- 参数：key 				数据名
-- 返回：n					状态码
--		s 					描述
this.delete_user_data = function()
	local t_params = ngx.ctx.t_params
	local t_user = mis_data.get_user()
	return t_user['delete_' .. t_params.key .. '_data'](t_user)
end

this.delete_user_data2 = function()
	local t_params = ngx.ctx.t_params
	local t_user = mis_data.get_user()
	return t_user['delete_' .. t_params.key .. '_data'](t_user, t_params.type)
end
--------------------------------------------------------------
-- 说明：添加补发任务
-- 参数：uin
--		from				来源
--		data 				数据
this.add_bufa_job = function()
	local t_params = ngx.ctx.t_params
	if mis_saveAss.add_job(t_params.uin, t_params.from, t_params.data) ~= t_retCode.n_OK then
		return t_retCode.n_SYS_ERR, 'add job failed'
	end
	return t_retCode.n_OK
end

--------------------------------------------------------------
-- 说明：增加生日领奖次数
this.add_birthday_gift = function()
	local t_params = ngx.ctx.t_params
	local n_update = t_params.add or 0
	local n_code, s_desc, t_user, t_userbirthday
	local t_birthdayConf = mis_data.t_birthday.t_conf
	local n_finish = 0

	if n_update == 1 then
		t_user = mis_data.get_user()
		n_code, t_userbirthday = t_user:get_birthday_data(true)
		if n_code ~= t_retCode.n_OK then
			return n_code, 'get user birthday info error'
		end

		-- 即将第几次生日
		n_finish = t_userbirthday.finish or 0
		t_userbirthday.gift = t_userbirthday.gift or {}

		n_finish 								= n_finish + 1
		t_userbirthday.finish 					= n_finish
		t_userbirthday.cake_list 				= t_userbirthday.cake_list or {}
		t_userbirthday.cake_list[n_finish] 		= t_birthdayConf.cake.num or 10
		t_userbirthday.cake_friend 				= t_userbirthday.cake_friend or {}
		t_userbirthday.cake_friend[n_finish] 	= t_userbirthday.cake_friend[n_finish] or {}
		t_userbirthday.gift 					= t_userbirthday.gift or {}
		t_userbirthday.gift[n_finish] 			= t_userbirthday.gift[n_finish] or {}

		mis_log.day('birthday', 'add_birthday_gift|' .. t_params.uin .. '|' .. t_params.ver .. '|' .. t_params.apiid .. '|' ..
				t_params.country .. '|' .. t_params.lang .. '|' .. '|' .. n_finish .. mis_common.table_to_json(t_userbirthday.gift))

		n_code, s_desc = t_user:set_birthday_data(t_userbirthday)
		if n_code ~= t_retCode.n_OK then
			return n_code, s_desc
		end
	end
	return t_retCode.n_OK, {finish = n_finish}
end

--------------------------------------------------------------
-- 说明：修改服务器时间
this.modify_server_time = function()
	local t_params = ngx.ctx.t_params
	local t_cache = ngx.shared.misCache
	local s_mission_type = t_params.mission_type or ''

	if t_cache then
		if t_params.stime and t_params.stime ~= 0 then
			-- 服务器时间，用于测试
			local n_stime = t_params.stime
			local b_success, s_err = t_cache:set(s_mission_type .. '_time' , n_stime)
			if not b_success then
				return t_retCode.n_OK, 'set ngx.shared.cache failed, ' .. s_err
			end
			return t_retCode.n_OK, {server_time = n_stime}
		end

		local b_success, s_err = t_cache:set(s_mission_type .. '_time' , 0)
		if not b_success then
			return t_retCode.n_OK, 'set ngx.shared.cache failed, ' .. s_err
		end
		return t_retCode.n_OK, {server_time = ngx.time()}
	end

	return t_retCode.n_OK, {server_time = ngx.time(), cache = 'not cache'}
end

--二期任务设置用户完成
this.set_twoback_user_event = function()
	mis_act.twoback_task_event()
	return t_retCode.n_OK,'success'
end

--二期任务重置用户信息到初始天
this.clear_twoback_user_day_time = function()
	local s_desc
	local t_user = mis_data.get_user()
	local n_code, t_twobackData = t_user:get_twoback_data()
	if n_code == t_retCode.n_OK then
		--t_twobackData.day_time = t_twobackData.loginTime or uu.now()
		t_twobackData.day_time = 0
		t_user:set_twoback_data(t_twobackData)
	end
	return t_retCode.n_OK,'success'
end

--二期任务重置用户信息到结束期
this.clear_twoback_user_end_time = function()
	local s_desc
	local t_user = mis_data.get_user()
	local n_code, t_twobackData = t_user:get_twoback_data()
	if n_code == t_retCode.n_OK then
		t_twobackData.get_twoback_task_time = 0
		t_user:set_twoback_data(t_twobackData)
	end
	return t_retCode.n_OK,'success'
end

--二期任务全部完成
this.do_twoback_user_all_task = function()
	local t_user = mis_data.get_user()
	local flag = false
	local n_code, t_twobackData = t_user:get_twoback_data()
	if n_code == t_retCode.n_OK then
		for k,v in pairs(t_twobackData.task) do
			if v.target_bonus then
				for i,j in pairs(v.target_bonus) do
					t_twobackData.task[k].target_bonus[i].do_num = t_twobackData.task[k].target_bonus[i].target
					t_twobackData.task[k].target_bonus[i].status = mis_config.t_missionStatus.n_DONE
					flag = true
				end
			end
		end

		if flag then
			t_user:set_twoback_data(t_twobackData)
		end

	end
	return t_retCode.n_OK,'success'
end

--二期任务重置用户信息
this.clear_twoback_user_all = function()
	local t_user = mis_data.get_user()
	local n_code, t_twobackData = t_user:get_twoback_data()
	if n_code == t_retCode.n_OK then
		t_user:set_twoback_data({})
		t_user:delete_back_data()
	end
	return t_retCode.n_OK,'success'
end


--重置用户中秋信息
this.clear_moon_data = function()
	local t_user = mis_data.get_user()
	local n_code, t_moonData = t_user:get_moon_data()
	if n_code == t_retCode.n_OK then
		t_user:set_moon_data({})
	end
	return t_retCode.n_OK,'success'
end

--二期任务设置用户为短流失
this.set_twoback_user_is_short = function()
	local t_user = mis_data.get_user()
	local n_code, t_twobackData = t_user:get_twoback_data()
	if n_code == t_retCode.n_OK then
		t_user:set_twoback_data({type=4,aimconf=mis_data.t_twoback.t_conf_short})
	end
	return t_retCode.n_OK,'success'
end

--二期任务设置用户为老回流
this.set_twoback_user_is_old_man = function()
	local t_user = mis_data.get_user()
	local n_code, t_twobackData = t_user:get_twoback_data()
	if n_code == t_retCode.n_OK then
		t_twobackData.old_or_new = false
		t_user:set_twoback_data(t_twobackData)
	end
	return t_retCode.n_OK,'success'
end

--二期任务设置用户为新回流
this.set_twoback_user_is_new_man = function()
	local t_user = mis_data.get_user()
	local n_code, t_twobackData = t_user:get_twoback_data()
	if n_code == t_retCode.n_OK then
		t_twobackData.old_or_new = true
		t_user:set_twoback_data(t_twobackData)
	end
	return t_retCode.n_OK,'success'
end


--二期任务获取用户所有信息
this.get_twoback_user_all_data = function()
	local t_user = mis_data.get_user()
	local n_code, t_twobackData = t_user:get_twoback_data()
	
	return t_retCode.n_OK,{t_twobackData=t_twobackData,t_conf_long=mis_data.t_twoback.t_conf_long,t_conf_short=mis_data.t_twoback.t_conf_short}
end

--国庆获取用户所有信息
this.get_nationalday_user_all_data = function()
	local t_user = mis_data.get_user()
	local n_code, t_nationaldayData = t_user:get_nationalday_data()
	
	return t_retCode.n_OK,{t_nationaldayData=t_nationaldayData,t_conf=mis_data.t_nationalday.t_conf}
end

--国庆重置用户信息到初始天
this.clear_nationalday_user_day_time = function()
	local s_desc
	local t_user = mis_data.get_user()
	local n_code, t_nationaldayData = t_user:get_nationalday_data()
	if n_code == t_retCode.n_OK then
		t_nationaldayData.day_time = 0
		t_user:set_nationalday_data(t_nationaldayData)
	end
	return t_retCode.n_OK,'success'
end

--国庆任务全部完成
this.do_nationalday_user_all_task = function()
	local t_user = mis_data.get_user()
	local flag = false
	local n_code, t_nationaldayData = t_user:get_nationalday_data()
	if n_code == t_retCode.n_OK then
		for k,v in pairs(t_nationaldayData.task) do
			t_nationaldayData.task[k].do_num = t_nationaldayData.task[k].target
			t_nationaldayData.task[k].status = mis_config.t_missionStatus.n_DONE
			flag = true
		end

		if flag then
			t_user:set_nationalday_data(t_nationaldayData)
		end

	end
	return t_retCode.n_OK,'success'
end

--国庆增加用户游园券
this.set_nationalday_user_money = function()
	local t_user = mis_data.get_user()
	local n_code, t_nationaldayData = t_user:get_nationalday_data()
	if n_code == t_retCode.n_OK then
		t_nationaldayData.total_coupons = 100000
		t_user:set_nationalday_data(t_nationaldayData)
	end

	return t_retCode.n_OK,{t_nationaldayData=t_nationaldayData,t_conf=mis_data.t_nationalday.t_conf}
end

--获取用户基础信息
this.get_basic_info = function()
	local t_user = mis_data.get_user()
	local n_code, _desc = t_user:get_user_basic()
	return t_retCode.n_OK,{n_code=n_code,_desc=_desc}
end

--增加音符
this.add_concert_note = function()
	local t_params 			= ngx.ctx.t_params
	local n_num				= t_params.num
	local t_user, t_userData, n_code, s_desc

	if n_num and mis_config.n_debug == 1 then
		t_user = mis_data.get_user()
		n_code, t_userData = t_user:get_concert_data()
		if n_code ~= t_retCode.n_OK then
			return n_code, 'get user concert data error'
		end

		t_userData.note_num = t_userData.note_num + n_num
		n_code, s_desc = t_user:set_concert_data(t_userData)
		if n_code ~= t_retCode.n_OK then
			return n_code, s_desc
		end
	end
	return t_retCode.n_OK, { note_num = t_userData.note_num }
end

--获取迷你号的md5
this.get_uin_sign = function()
	if mis_config.n_debug ~= 1 then
		return t_retCode.n_OPER_ERR, "error"
	end
	local t_params 			= ngx.ctx.t_params
	local s_s2t  			= t_params.check_s2t or t_params.s2t
	local n_time  			= tonumber(t_params.check_time or t_params.time)
	local b_isAct			= t_params.useAct and true or false
	local s_genMd5 			= mis_calcu.gen_md5_1(t_params.check_uin, n_time, s_s2t, b_isAct)
	return t_retCode.n_OK, { sign = s_genMd5 }
end

--增加兑奖券
this.add_share_mission_award = function()
	local t_params 			= ngx.ctx.t_params
	local s_type			= t_params.mission_type
	local n_award_num		= t_params.award_num
	local n_exchange_num	= t_params.exchange_num
	local t_user, t_userData, n_code, s_desc

	if s_type and n_award_num and n_exchange_num and mis_config.n_debug == 1 then
		t_user = mis_data.get_user()
		n_code, t_userData = t_user:get_share_mission_data(s_type)
		if n_code ~= t_retCode.n_OK then
			return n_code, 'get user concert data error'
		end
		mis_log.debug('add_share_mission_award:' .. mis_common.table_to_json(t_userData))

		t_userData.award_num = t_userData.award_num or {}
		t_userData.award_num[s_type] = t_userData.award_num[s_type] or 0
		t_userData.award_num[s_type] = t_userData.award_num[s_type] + n_award_num

		t_userData.exchange_num = t_userData.exchange_num or 0
		t_userData.exchange_num = t_userData.exchange_num + n_exchange_num

		n_code, s_desc = t_user:set_share_mission_data(s_type, t_userData)
		if n_code ~= t_retCode.n_OK then
			return n_code, s_desc
		end
	end

	return t_retCode.n_OK, { exchange_num = t_userData.exchange_num }
end

-- 云服上报成功挑战年兽事件
this.year_beast_event = function()
	local t_params 			= ngx.ctx.t_params
	local s_type			= mis_config.t_workType.s_YEAR_BEAST
	local n_uin 			= t_params.uin
	local s_roomId			= t_params.roomId
	local s_uinList			= t_params.uinList
	local t_uinList			= mis_uu.split( s_uinList, ',' )
	local t_userList		= {}
	local s_inCache			= '0'
	local n_code, s_desc, t_lock, t_gobal_lock
	local t_cache = ngx.shared.misSysUser

	local s_gobal_key  	= mis_config.t_year_beast.s_gobal_key
	local s_gobal_lock  = mis_config.t_year_beast.s_gobal_lock
	local s_event_key  	= mis_config.t_year_beast.s_event_key
	local s_event_lock 	= mis_config.t_year_beast.s_event_lock

	if #t_uinList > 0 and t_cache then
		-------------- 全服次数统计 --------------
		n_code, t_gobal_lock = mis_lock.lock(s_gobal_lock)
		if n_code ~= t_retCode.n_OK then
			mis_log.day("year_beast_error","year_beast_event gobal_lock error|" .. s_type .. '|' .. n_code)
			return n_code, 'year_beast_event lock error'
		end

		t_cache:incr(s_gobal_key, 1, 0)
		mis_lock.unlock(t_gobal_lock)

		-------------- 全服次数统计 --------------

		n_code, t_lock = mis_lock.lock(s_event_lock)
		if n_code ~= t_retCode.n_OK then
			mis_log.day("year_beast_error","year_beast_event lock error|" .. s_type .. '|' .. n_code)
			return n_code, 'year_beast_event lock error'
		end

		local s_user = t_cache:get(s_event_key)
		if s_user then
			t_userList = table.loadstring(s_user)
		end

		table.insert(t_userList, t_uinList)

		n_code, s_desc = t_cache:set(s_event_key , table.tostring(t_userList))
		if not n_code then
			mis_log.day("year_beast_error","set cache error|" .. t_params.uin .. '|' .. s_type)
		else
			s_inCache = '1'
		end

		mis_lock.unlock(t_lock)

		mis_log.day("year_beast", "year_beast_event|" .. n_uin .. '|' .. s_roomId .. '|' .. s_uinList .. '|inCache=' .. s_inCache .. '' .. #t_userList)
	end

	return t_retCode.n_OK
end

-- 说明：服务器上报战年兽事件
this.share_mission_event = function()
	return mis_act.share_mission_event()
end

--迎财神设置用户状态为3
this.setfuck_god_sta= function()
	local t_user = mis_data.get_user()
	local n_code, Data = t_user:get_share_mission_data('wealth_god')
	if n_code == t_retCode.n_OK then
		Data.wealth_god_data.status = mis_config.t_wealthgodStatus.n_status3
		Data.wealth_god_data.have_qu = 1
		t_user:set_share_mission_data('wealth_god',Data)
	end

	return t_retCode.n_OK,{Data=Data}
end

--迎财神清掉数据
this.clearfuck_god_data= function()
	local t_user = mis_data.get_user()
	local n_code, Data = t_user:get_share_mission_data('wealth_god')
	if n_code == t_retCode.n_OK then
		Data.wealth_god_data = nil
		t_user:set_share_mission_data('wealth_god',Data)
	end
	return t_retCode.n_OK,{Data=Data}
end

--迎财神获取所有数据
this.getfuck_god_data_all= function()
	local t_user = mis_data.get_user()
	local n_code, Data = t_user:get_share_mission_data('wealth_god',true)
	local code_1,data_2 = mis_data.wealth_god_get_award_pool(true)
	return t_retCode.n_OK,{Data=Data,data_2=data_2,conf=mis_uu.table_value_copy(mis_data.t_share_mission['wealth_god'].t_conf)}
end

--获取迎财神助力码数据
this.getfuck_god_data_code_all= function()
	if ngx.ctx.t_params.wealth_god_code then 
		local t_sysUser 		= mis_data.get_user(0)
		local appoint_code =  string.sub(ngx.ctx.t_params.wealth_god_code, 13,#ngx.ctx.t_params.wealth_god_code)
		local n_code, t_codeData = t_sysUser:get_wealth_god_code_data(appoint_code,true)
		if n_code == t_retCode.n_OK then
			
			return n_code,t_codeData
		end
	end
	return t_retCode.n_OK
end

--获取花小楼所有数据
this.getfuck_flowerxiaolou_data_all= function()
	local t_user = mis_data.get_user()
	local n_code, Data = t_user:get_share_mission_data('flower_xiaolou',true)
	
	return t_retCode.n_OK,{Data=Data}
end

--清除花小楼所有数据
this.clearfuck_flowerxiaolou_data= function()
	local t_user = mis_data.get_user()
	local n_code, Data = t_user:get_share_mission_data('flower_xiaolou')
	if n_code == t_retCode.n_OK then
		Data = {}
		t_user:set_share_mission_data('flower_xiaolou',Data)
	end
	return t_retCode.n_OK,{Data=Data}
end

--重置迎财神奖池接口
this.clearfuck_god_pool_data= function()
	local code_1,data_2 = mis_data.wealth_god_get_award_pool()

	if code_1 == t_retCode.n_OK then
		mis_data.wealth_god_set_award_pool({})
	end

	return t_retCode.n_OK,"success"
end

--修改玩家迎财神预约时间
this.setfuck_god_redtime_data= function()
	local t_user = mis_data.get_user()
	local n_code, Data = t_user:get_share_mission_data('wealth_god')

	Data.tasks['wealth_god'][1].refreshTime = Data.tasks['wealth_god'][1].refreshTime-86400
	t_user:set_share_mission_data('wealth_god',Data)

	return t_retCode.n_OK,{Data=Data}
end

--查询玩家创建游戏和充值记录
this.checkfuck_user_creattime_recharge_data= function()
	local check_low_valueuser = mis_bridge.query_owned_creat_recharge(ngx.ctx.t_params.uin)

	return t_retCode.n_OK,{Data=check_low_valueuser}
end

--查询玩家新手任务信息
this.checkfuck_novice_status= function()
	local n_code, t_user, t_userData

	t_user = mis_data.get_user()
	n_code, t_userData = t_user:get_novice_fuck_data()
	return t_retCode.n_OK,{Data=t_userData}
end

-- 认证成为创作者
this.certified_creator = function()
	ngx.ctx.t_params.event 		= 51
	ngx.ctx.t_params.value 		= 1
	ngx.ctx.t_params.level 		= 0
	ngx.ctx.t_params.curr_level = 0
	ngx.ctx.t_params.mission_type = 'creator'

	return mis_act.level_mission_event()
end

-- push-签到及任务活动数据批量查询接口
this.get_mission_data_batch = function()
	local t_params 			= ngx.ctx.t_params
	local s_uin_list		= t_params.uin_list
	local s_task_list		= t_params.task_list
	local n_count			= 0
	local t_data			= {}
	local n_nowtime 		= ngx.time()

	local t_uin_list		= mis_uu.split( s_uin_list, ',' )
	local t_task_list		= mis_uu.split( s_task_list, ',' )

	local n_code, t_user, t_userData

	for i = 1, #t_uin_list do
		local t_task_data = {}
		local n_uin = tonumber(t_uin_list[i])
		local b_twoback = false

		for id = 1, #t_task_list do
			local t_tmp = {}
			local s_task = t_task_list[id]
			if s_task == '1' then		-- 每日签到
				t_user = mis_data.get_user(n_uin)
				n_code, t_userData = t_user:get_daily_sign_data(true)
				if n_code == t_retCode.n_OK and t_userData.week_sign and t_userData.refresh_time and not mis_common.is_another_week(t_userData.refresh_time, n_nowtime) then
					t_tmp = { activity_id = s_task, state = t_userData.week_sign}
				end
			elseif s_task == '2' then		-- 新手任务
				t_user = mis_data.get_user(n_uin)
				n_code, t_userData = t_user:get_novice_task_data(true)
				if n_code == t_retCode.n_OK then
					t_tmp = { activity_id = s_task, state = t_userData.final_award_status or 0}
				end
			elseif not b_twoback and (s_task == '3' or s_task == '4') then	-- 长回流、短回流
				t_user = mis_data.get_user(n_uin)
				n_code, t_userData = t_user:get_twoback_data(true)
				if n_code == t_retCode.n_OK and t_userData.type and t_userData.task then
					local n_state = 0

					for _, v in pairs(t_userData.task) do
						if v and v.event == 7 and v.target_bonus then
							n_state = v.target_bonus[1] and v.target_bonus[1].status or 0
						end
					end

					if t_userData.type == 3 then
						b_twoback = true
						t_tmp = { activity_id = t_userData.type, state = n_state}
					elseif t_userData.type == 4 then
						b_twoback = true
						t_tmp = { activity_id = t_userData.type, state = n_state}
					end
				end
			else
				mis_log.day( "mission_batch", "task_error " .. s_task)
			end

			if not mis_uu.tableIsEmpty(t_tmp) then
				n_count = n_count + 1
				table.insert(t_task_data, t_tmp)
			end
		end

		if n_count > 50 then
			mis_log.day( "mission_batch", "list_big " .. #t_uin_list)
			break
		end

		t_data[n_uin] = t_task_data
	end

	return t_retCode.n_OK, t_data
end

--删除新手，签到
this.debug_clear_mission = function()
	
	local server_ip = ngx.ctx.xff or ngx.var.remote_addr or "nil"
	if mis_config.n_debug == 1 or mis_config.cmd_white_ip[server_ip] == 1 then

	local t_user = mis_data.get_user()
	local n_code, t_Data = t_user:get_daily_sign_data()
	if n_code == t_retCode.n_OK then

		if uu.tableNotEmpty(t_Data) then
        	--有数据才记录并删除
        	this.write_log("mission_sign",t_Data)
    	end
		t_user:set_daily_sign_data({})
	end

	n_code, t_Data = t_user:get_novice_task_data()
	if n_code == t_retCode.n_OK then

		if uu.tableNotEmpty(t_Data) then
        	--有数据才记录并删除
        	this.write_log("mission_novice",t_Data)
    	end
		t_user:set_novice_task_data({})
	end

	n_code, t_Data = t_user:get_birthday_data()
	if n_code == t_retCode.n_OK then

		if uu.tableNotEmpty(t_Data) then
        	--有数据才记录并删除
        	this.write_log("mission_birthday",t_Data)
    	end
		t_user:set_birthday_data({})
	end

	n_code, t_Data = t_user:get_twoback_data()
	if n_code == t_retCode.n_OK then

		if uu.tableNotEmpty(t_Data) then
        	--有数据才记录并删除
        	this.write_log("mission_twoback",t_Data)
    	end
		t_user:set_twoback_data({})
	end

	return t_retCode.n_OK,'success'
	end
	log.day_list("fuckmis","debug_clear_mission-error",ngx.ctx.t_params.uin,uu.to_str(ngx.ctx.xff),uu.to_str(ngx.var.remote_addr))
	return -1,'error_cmdip'
end
this.debug_recycle_mission = function()

	local server_ip = ngx.ctx.xff or ngx.var.remote_addr or "nil"
	if mis_config.n_debug == 1 or mis_config.cmd_white_ip[server_ip] == 1 then

	local t_user = mis_data.get_user()
	local n_code, t_Data = t_user:get_daily_sign_data()
	if n_code == t_retCode.n_OK then
		local retc,retm = this.read_log("mission_sign")
		if retc then
			t_user:set_daily_sign_data(retm)
		end
	end
	
	n_code, t_Data = t_user:get_novice_task_data()
	if n_code == t_retCode.n_OK then
		local retc,retm = this.read_log("mission_novice")
		if retc then
			t_user:set_novice_task_data(retm)
		end
	end

	n_code, t_Data = t_user:get_birthday_data()
	if n_code == t_retCode.n_OK then
		local retc,retm = this.read_log("mission_birthday")
		if retc then
			t_user:set_birthday_data(retm)
		end
	end
	
	n_code, t_Data = t_user:get_twoback_data()
	if n_code == t_retCode.n_OK then
		local retc,retm = this.read_log("mission_twoback")
		if retc then
			t_user:set_twoback_data(retm)
		end
	end

	return t_retCode.n_OK,'success'
	end
	log.day_list("fuckmis","debug_recycle_mission-error",ngx.ctx.t_params.uin,uu.to_str(ngx.ctx.xff),uu.to_str(ngx.var.remote_addr))
	return -1,'error_cmdip'
end

--写入log文件保存
this.write_log = function(keyname,data)
    local path_ = 'logs/miniw/' .. ngx.ctx.t_params.uin .. "_"..keyname  .. '_zhushan.log';
    local strdata =table.tostring(data)
    local file = io.open(path_,'a')
    if  file then
        file:write( strdata );
        file:close();
    end
end
--获取文件恢复
this.read_log = function(keyname)
    local path_ = 'logs/miniw/' .. ngx.ctx.t_params.uin .. "_"..keyname  .. '_zhushan.log';
    local file = io.open(path_, "r");  
    if file then
        local txt = file:read("*a")
        file:close();
        local t = table.loadstring(txt)
        local cmd_check_ = "rm "..path_
		local ret_ = uu.os_exec( cmd_check_ )
        return true,t
    end
    return false,{}
end


return this